package dao.impl;

import dao.OrdersDao;
import model.Orders;
import model.User;
import utils.DaoUtil;
import utils.GenicaDaoImpl;
import vo.OrderVo;

import java.util.List;

public class OrdersDaoImpl extends GenicaDaoImpl<Orders,Integer> implements OrdersDao {
    @Override
    public List<Orders> findByUserId(int id,int state) {
        List<Orders> orders=null;
        String sql = "select * from "+ DaoUtil.getTable(Orders.class);
        if(id!=0){
            sql+=" where user_id=?";
        }

        if(state!=-1&&state!=2){
          sql = sql+" and state=?";
          orders = DaoUtil.queryList(Orders.class,sql,id,state);
        }else if (state==2){
          sql = sql+" and state=? or state=?";
          orders = DaoUtil.queryList(Orders.class,sql,id,2,3);
        }else if(state==-1){
              orders=DaoUtil.queryList(Orders.class,sql,id);
        }else {
            orders = DaoUtil.queryList(Orders.class,sql,id);
        }
        return orders;
    }

    @Override
    public Orders findOrderByOrderNo(String order_no) {
        String sql = "select * from "+DaoUtil.getTable(Orders.class)+" where order_no =?";
        return DaoUtil.query(Orders.class,sql,order_no);
    }

    @Override
    public boolean updateOrderStateById(String order_no, int state) {
        String sql = "update "+DaoUtil.getTable(Orders.class)+" set state=? where order_no=?";
        return DaoUtil.update(sql,state,order_no);
    }

    @Override
    public List<Orders> findAll(int page, int limit) {
        page = (page-1)*limit;
       String sql = "select * from orders limit ?,?";
        return DaoUtil.queryList(Orders.class,sql,page,limit);
    }

    @Override
    public int findCount() {
        String sql = "select count(*) from "+DaoUtil.getTable(Orders.class);
        return Integer.parseInt(DaoUtil.getOne(sql).toString());
    }

    @Override
    public int findCount(int state) {
        String sql = "select count(*) from "+DaoUtil.getTable(Orders.class)+" where state=?";
        return Integer.parseInt(DaoUtil.getOne(sql,state).toString());
    }

    @Override
    public List<Orders> findAllByState(int state, int page, int limit) {
        page = (page-1)*limit;
        String sql = "select * from orders where state = ? limit ?,?";
        return DaoUtil.queryList(Orders.class,sql,state,page,limit);
    }
}
